package com.wuyoucar.service.impl;

import com.sun.tracing.dtrace.ArgsAttributes;
import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TCarinfo;
import com.wuyoucar.entity.TCarinfoSource;
import com.wuyoucar.mapper.TCarinfoMapper;
import com.wuyoucar.service.ICarinfoSourceService;
import com.wuyoucar.service.InCarinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CarinfoServiceImpl implements InCarinfoService {
    @Autowired
    TCarinfoMapper tCarinfoMapper;
    @Autowired
    private ICarinfoSourceService carinfoSourceService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //绑定车辆
    @Override
    public ResultBean insertCarinfo(TCarinfo tCarinfo) {
        String carnumber = tCarinfo.getCarnumber();
        String vin = tCarinfo.getVin();
        Map<String,String> map = new HashMap<>();
        map.put("vin",vin);
        map.put("carnumber",carnumber);
        TCarinfoSource carByCarNumber = carinfoSourceService.findCarByCarNumber(map);
        if (carByCarNumber == null){
            return new ResultBean(404,"绑定失败,请确保填写的信息真实有效");
        }else{
            int n = tCarinfoMapper.insertCarinfo(tCarinfo);
            return new ResultBean(200,"操作成功");
        }

    }

    //根据用户id查询所有车辆
    @Override
    public ResultBean selectAllCarById(Integer userid) {
        List<TCarinfo> tCarinfos = tCarinfoMapper.selectAllCarById(userid);
        if (tCarinfos.size()>0){
            return new ResultBean(200,"操作成功",tCarinfos);
        }else{
            return new ResultBean(404,"您还没有绑定车辆哦!");
        }

    }

    @Override
    public ResultBean unbinding(String vin) {

        int unbunding = tCarinfoMapper.unbunding(vin);
        if (unbunding>0){
            return new ResultBean(200,"操作成功");
        }else{
            return new ResultBean(404,"操作失败");
        }

    }

}
